<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Exercise 4.3: Solve a simple QP with inequality constraints</title>
<link rel="canonical" href="/Users/mcgrant/Projects/CVX/examples/cvxbook/Ch04_cvx_opt_probs/html/ex_4_3.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Exercise 4.3: Solve a simple QP with inequality constraints</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% From Boyd &amp; Vandenberghe, "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 09/26/05</span>
<span class="comment">%</span>
<span class="comment">% Solves the following QP with inequality constraints:</span>
<span class="comment">%           minimize    1/2x'*P*x + q'*x + r</span>
<span class="comment">%               s.t.    -1 &lt;= x_i &lt;= 1      for i = 1,2,3</span>
<span class="comment">% Also shows that the given x_star is indeed optimal</span>

<span class="comment">% Generate data</span>
P = [13 12 -2; 12 17 6; -2 6 12];
q = [-22; -14.5; 13];
r = 1;
n = 3;
x_star = [1;1/2;-1];

<span class="comment">% Construct and solve the model</span>
fprintf(1,<span class="string">'Computing the optimal solution ...'</span>);
cvx_begin
    variable <span class="string">x(n)</span>
    minimize ( (1/2)*quad_form(x,P) + q'*x + r)
    x &gt;= -1;
    x &lt;=  1;
cvx_end
fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Display results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The computed optimal solution is: '</span>);
disp(x);
disp(<span class="string">'The given optimal solution is: '</span>);
disp(x_star);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the optimal solution ... 
Calling SDPT3 4.0: 11 variables, 4 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints =  4
 dim. of socp   var  =  5,   num. of socp blk  =  1
 dim. of linear var  =  6
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|9.5e-01|4.3e+00|2.3e+03| 2.204541e+02  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|1.000|4.5e-07|5.0e-02|2.0e+02| 2.080252e+02  2.878193e+01| 0:0:00| chol  1  1 
 2|0.943|0.925|6.8e-08|8.4e-03|1.2e+01| 4.760357e+01  3.641164e+01| 0:0:00| chol  1  1 
 3|0.791|1.000|4.1e-07|5.0e-04|5.7e+00| 4.293607e+01  3.726829e+01| 0:0:00| chol  1  1 
 4|1.000|0.888|5.7e-08|1.0e-04|1.4e+00| 4.015948e+01  3.881176e+01| 0:0:00| chol  1  1 
 5|0.883|1.000|9.4e-09|5.0e-06|3.0e-01| 3.931792e+01  3.901936e+01| 0:0:00| chol  1  1 
 6|1.000|0.983|2.6e-09|5.8e-07|1.6e-02| 3.913463e+01  3.911827e+01| 0:0:00| chol  1  1 
 7|0.977|0.972|3.3e-10|6.5e-08|4.2e-04| 3.912522e+01  3.912480e+01| 0:0:00| chol  1  1 
 8|0.988|0.988|8.6e-12|8.7e-10|5.2e-06| 3.912500e+01  3.912500e+01| 0:0:00| chol  1  1 
 9|0.996|0.994|6.5e-12|6.9e-12|7.3e-08| 3.912500e+01  3.912500e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  9
 primal objective value =  3.91250000e+01
 dual   objective value =  3.91250000e+01
 gap := trace(XZ)       = 7.33e-08
 relative gap           = 9.25e-10
 actual relative gap    = 9.24e-10
 rel. primal infeas (scaled problem)   = 6.53e-12
 rel. dual     "        "       "      = 6.87e-12
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 3.8e+01, 3.1e+00, 4.2e+00
 norm(A), norm(b), norm(C) = 4.3e+00, 3.4e+01, 5.3e+00
 Total CPU time (secs)  = 0.12  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 9.5e-12  0.0e+00  1.2e-11  0.0e+00  9.2e-10  9.3e-10
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -21.625
 
Done! 
------------------------------------------------------------------------
The computed optimal solution is: 
    1.0000
    0.5000
   -1.0000

The given optimal solution is: 
    1.0000
    0.5000
   -1.0000

</pre>
</div>
</body>
</html>